package com.topscomm.mpm.script;

import cn.hutool.core.collection.CollectionUtil;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.service.ICboMessageAppService;
import com.topscomm.cbo.service.ICboMessageSysService;
import com.topscomm.cbo.service.impl.CboMessageAppService;
import com.topscomm.cbo.service.impl.CboMessageSysService;
import com.topscomm.mis.pojo.MisDeptHeaderEntity;
import com.topscomm.mpm.MpmScript;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.pojo.MpmProjectSuperviseEntity;
import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.mpm.service.impl.MpmProjectSuperviseService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author wudaliang
 * @date 2021/7/30
 * @description:
 * @modify:
 */
public class MpmProjectSuperviseScript extends MpmScript {

    /**
     * @author: wudaliang
     * @date: 2021-07-30
     * @description: 项目监督
     * @return: void
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateProjectSupervise(ITapService service, TapEntity entity) {
        //审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());

        MpmProjectSuperviseService mpmProjectSuperviseService = (MpmProjectSuperviseService) SpringUtil
                .getBean("mpmProjectSuperviseService");
        MpmProjectSuperviseEntity mpmProjectSuperviseEntity = mpmProjectSuperviseService
                .queryById(sourceid);
        mpmProjectSuperviseEntity.setSupervisestate("2");
        mpmProjectSuperviseService.updateEntity(mpmProjectSuperviseEntity);
    }

    /**
     * @author: houyuhang
     * @date: 2021-09-06
     * @description: 督办审批开始后提示财总。
     * @return: com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void remindCFO(ITapService service, TapEntity entity) {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        // sourcetype = 'MpmProjectSupervise'
        String sourcetype = ConvertUtil.convertToString(nodeins.getSourcetype());
        MpmProjectSuperviseService mpmProjectSuperviseService = (MpmProjectSuperviseService) SpringUtil.getBean("mpmProjectSuperviseService");
        ICboMessageAppService cboMessageAppService = (ICboMessageAppService) SpringUtil.getBean("cboMessageAppService");
        ICboMessageSysService cboMessageSysService = (ICboMessageSysService) SpringUtil.getBean("cboMessageSysService");
        //查询项目entity
        MpmProjectSuperviseEntity mpmProjectSuperviseEntity = mpmProjectSuperviseService.queryById(sourceid);
        // 如果是系统发送的督办，就不用给CFO发送消息
        if (mpmProjectSuperviseEntity.getSendresource() == 1) {
            return;
        }
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("id", mpmProjectSuperviseEntity.getId());
        String selectName = "getCFOCode";
        List<Map<String, Object>> res = mpmProjectSuperviseService.queryMap(paraMap, selectName);
        if (CollectionUtil.isEmpty(res)) {
            return;
        }
        // 插入App消息
        String usercode = res.get(0).get("usercode").toString();
        String username = res.get(0).get("username").toString();
        String title = "销售项目督办单提醒";
        String content = "【" + mpmProjectSuperviseEntity.getProjectname() + "】督办单:" +
                mpmProjectSuperviseEntity.getSupervisename() + "，已下发，责任人" +
                mpmProjectSuperviseEntity.getResponsiblename() + "，请知悉";
        Map<String, Object> projectappmap = this.insertMessageApp(usercode, title, content,
                ConvertUtil.convertToLong(sourceid), sourcetype, 0);
        cboMessageAppService.insertMap(projectappmap);
        // 系统消息
        Map<String, Object> sysMap = this.insertMessageSys(usercode, username, title, content,
                ConvertUtil.convertToLong(sourceid), sourcetype);
        cboMessageSysService.insertMap(sysMap);
    }

    /**
     * @author: zhangqianbo
     * @date:
     * @description: 督办PMO审批通过后提示项目经理、省总和财总。
     * @return: com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @modify:
     */
    @Transactional
    public void remindManagerCEOCFO(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        String sourcetype = ConvertUtil.convertToString(nodeins.getSourcetype());
        MpmProjectSuperviseService mpmProjectSuperviseService = (MpmProjectSuperviseService) SpringUtil.getBean("mpmProjectSuperviseService");
        MpmProjectService mpmProjectService = (MpmProjectService) SpringUtil.getBean("mpmProjectService");
        ICboMessageAppService cboMessageAppService = (ICboMessageAppService) SpringUtil.getBean("cboMessageAppService");
        ICboMessageSysService cboMessageSysService = (ICboMessageSysService) SpringUtil.getBean("cboMessageSysService");
        // 查询项目督办entity
        MpmProjectSuperviseEntity mpmProjectSuperviseEntity = mpmProjectSuperviseService.queryById(sourceid);
        // 查询项目entity
        StringBufferProxy str = new StringBufferProxy();
        str.appendSingle("projectcode = '{0}'", mpmProjectSuperviseEntity.getProjectcode());
        List<MpmProjectEntity> mpmProjectEntities = mpmProjectService.queryByWhere(str.toString());
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("id", mpmProjectSuperviseEntity.getId());
        // 查询CEO和CFO信息
        List<Map<String, Object>> cfoInfo = mpmProjectSuperviseService.queryMap(paraMap, "getCFOCode");
        List<Map<String, Object>> ceoInfo = mpmProjectSuperviseService.queryMap(paraMap, "getCEOCode");
        String cfoUserCode = cfoInfo.get(0).get(MisDeptHeaderEntity.FieldUsercode).toString();
        String cfoUserName = cfoInfo.get(0).get(MisDeptHeaderEntity.FieldUsername).toString();
        String ceoUserCode = ceoInfo.get(0).get(MisDeptHeaderEntity.FieldUsercode).toString();
        String ceoUserName = ceoInfo.get(0).get(MisDeptHeaderEntity.FieldUsername).toString();
        String projectManagerCode = mpmProjectEntities.get(0).getProjectmanagercode();
        String projectManager = mpmProjectEntities.get(0).getProjectmanager();

        // 插入App消息
        String title = "销售项目督办单提醒";
        String content = "【" + mpmProjectSuperviseEntity.getProjectname() + "】督办单:" +
                mpmProjectSuperviseEntity.getSupervisename() + "，已下发，责任人" +
                mpmProjectSuperviseEntity.getResponsiblename() + "，请知悉";
        Map<String, Object> cfoAppMap = insertMessageApp(cfoUserCode, title, content, ConvertUtil.convertToLong(sourceid), sourcetype, 0);
        Map<String, Object> ceoAppMap = insertMessageApp(ceoUserCode, title, content, ConvertUtil.convertToLong(sourceid), sourcetype, 0);
        Map<String, Object> projectMangerAppMap = insertMessageApp(projectManagerCode, title, content, ConvertUtil.convertToLong(sourceid), sourcetype, 0);
        cboMessageAppService.insertMap(cfoAppMap);
        cboMessageAppService.insertMap(ceoAppMap);
        cboMessageAppService.insertMap(projectMangerAppMap);

        // 系统消息
        Map<String, Object> ceoSysMap = insertMessageSys(ceoUserCode, ceoUserName, title, content, ConvertUtil.convertToLong(sourceid), sourcetype);
        Map<String, Object> cfoSysMap = insertMessageSys(cfoUserCode, cfoUserName, title, content, ConvertUtil.convertToLong(sourceid), sourcetype);
        Map<String, Object> projectManagerSysMap = insertMessageSys(projectManagerCode, projectManager, title, content, ConvertUtil.convertToLong(sourceid), sourcetype);
        cboMessageSysService.insertMap(ceoSysMap);
        cboMessageSysService.insertMap(cfoSysMap);
        cboMessageSysService.insertMap(projectManagerSysMap);

        // 如果PMO审核节点选择了抄送人，就给抄送人发送app消息提醒
        mpmProjectSuperviseService.sendAppMessageToCcStaff(paraMap);
    }

    /**
     * @author: zhangqianbo
     * @date:
     * @description: 插入系统消息
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @modify:
     */
    public Map<String, Object> insertMessageSys(String usercode, String username, String title, String content, long sourceid, String sourcetype) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("receivercode", usercode);
        map.put("receivername", username);
        map.put("subject", title);
        map.put("content", content);
        map.put("sourceid", sourceid);
        map.put("sourcetype", sourcetype);
        return map;
    }

    /**
     * @author: houyuhang
     * @date: 2021-09-06
     * @description: 插入App消息
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @modify:
     */
    public Map<String, Object> insertMessageApp(String usercode, String extendfield1, String content, long sourceid, String sourcetype, int sendtype) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("usercode", usercode);
        map.put("extendfield1", extendfield1);
        map.put("content", content);
        map.put("sourceid", sourceid);
        map.put("sourcetype", sourcetype);
        map.put("sendtype", sendtype);
        return map;
    }
}
